<Project Sdk="Microsoft.Build.NoTargets">
  <PropertyGroup>
    <SkipBuild Condition="'$(RuntimeFlavor)' != '$(PrimaryRuntimeFlavor)' or '$(TargetsMobile)' == 'true'">true</SkipBuild>
    <GenerateInstallers>true</GenerateInstallers>
    <InstallerName>dotnet-host</InstallerName>
    <InstallerName Condition="'$(TargetOS)' == 'osx'">dotnet-host-internal</InstallerName>
    <LinuxInstallRoot Condition="'$(BuildRpmPackage)' == 'true'">/</LinuxInstallRoot>
    <PackageBrandNameSuffix>Host</PackageBrandNameSuffix>
    <VSInsertionShortComponentName>NetCore.SharedHost</VSInsertionShortComponentName>
    <UseCustomDirectoryHarvesting>true</UseCustomDirectoryHarvesting>
    <WixIncludeRegistryKeys>true</WixIncludeRegistryKeys>
    <RegKeyProductName>sharedhost</RegKeyProductName>
    <!-- Contributes to DependencyKey which ensures stable provider key - do not change -->
    <WixDependencyKeyName>Dotnet_CLI_SharedHost</WixDependencyKeyName>
    <OutputFilesCandleVariable>HostSrc</OutputFilesCandleVariable>
    <MajorUpgradeSchedule>afterInstallExecute</MajorUpgradeSchedule>
    <VersionInstallerName>false</VersionInstallerName>
    <UseBrandingNameInLinuxPackageDescription>true</UseBrandingNameInLinuxPackageDescription>
    <MacOSComponentNamePackType>sharedhost</MacOSComponentNamePackType>
    <IncludeVersionInMacOSComponentName>false</IncludeVersionInMacOSComponentName>
    <MacOSScriptsTemplateDirectory>osx_scripts/host</MacOSScriptsTemplateDirectory>
    <MacOSPackageDescription>The .NET Shared Host.</MacOSPackageDescription>
    <!-- Enables stable upgrade code - do not change -->
    <MsiUpgradeCodeSeed>dotnet-host $(MajorVersion).$(MinorVersion) $(Platform)</MsiUpgradeCodeSeed>
  </PropertyGroup>

  <ItemGroup>
    <WixSrcFile Include="host.wxs" />
    <WixExtraComponentGroupRefId Include="InstallSharedHostandDetectionKeys" />
    <CandleVariables Include="ExtraPropertyRefIds" Value="ProductCPU;RTM_ProductVersion" />
    <!-- Enables stable provider key - do not change -->
    <CandleVariables Include="DependencyKey" Value="$(WixDependencyKeyName)_$(MajorVersion).$(MinorVersion)_$(TargetArchitecture)" />
  </ItemGroup>

  <Target Name="PublishToDisk">
    <Error Condition="'$(OutputPath)' == ''" Text="Publishing to disk requires the OutputPath to be set to the root of the path to write to." />

    <PropertyGroup>
      <_DestinationPath>$(OutputPath)</_DestinationPath>
      <_DestinationPath Condition="'$(BuildRpmPackage)' == 'true'">$(OutputPath)usr/share/dotnet/</_DestinationPath>
      <_UsrBinPath>$(OutputPath)usr/bin/</_UsrBinPath>
    </PropertyGroup>

    <MakeDir Condition="'$(BuildRpmPackage)' == 'true'" Directories="$(_DestinationPath)" />
    <MakeDir Condition="'$(BuildRpmPackage)' == 'true'" Directories="$(_UsrBinPath)" />

    <ItemGroup>
      <FilesToPublish Include="$(DotNetHostBinDir)\dotnet$(ExeSuffix)"
            Destination="$(_DestinationPath)dotnet$(ExeSuffix)" />
      <FilesToPublish Include="$(InstallerProjectRoot)pkg\THIRD-PARTY-NOTICES.TXT"
            Destination="$(_DestinationPath)ThirdPartyNotices.txt" />
      <FilesToPublish Include="$(RepoRoot)LICENSE.TXT"
            Destination="$(_DestinationPath)LICENSE.txt"
            Condition="'$(TargetsUnix)' == 'true'"/>
      <FilesToPublish Include="$(InstallerProjectRoot)pkg\LICENSE-MSFT.TXT"
            Destination="$(_DestinationPath)LICENSE.txt"
            Condition="'$(TargetsUnix)' != 'true'"/>
    </ItemGroup>

    <Copy SourceFiles="@(FilesToPublish)"
          DestinationFiles="%(FilesToPublish.Destination)" />

    <!--
      Create symlink so the dotnet host is in PATH. It points to where dotnet will be installed, so
      it may seem to point to the wrong place in the build machine's layout.
    -->
    <Exec Condition="'$(BuildRpmPackage)' == 'true'" Command="ln -sf &quot;/usr/share/dotnet/dotnet&quot; &quot;$(_UsrBinPath)dotnet&quot;" />

  </Target>

  <Target Name="PublishSymbolsToDisk">
    <Error Condition="'$(SymbolsOutputPath)' == ''" Text="Publishing to disk requires the SymbolsOutputPath to be set to the root of the path to write to." />

    <Copy SourceFiles="$(DotNetHostBinDir)\dotnet.pdb"
          Condition="'$(TargetOS)' == 'windows'"
          DestinationFolder="$(SymbolsOutputPath)" />

    <Copy SourceFiles="$(DotNetHostBinDir)\dotnet$(SymbolsSuffix)"
          Condition="'$(TargetOS)' != 'windows'"
          DestinationFolder="$(SymbolsOutputPath)" />
  </Target>

  <Target Name="AddLinuxPackageInformation" BeforeTargets="GetDebInstallerJsonProperties;GetRpmInstallerJsonProperties">
    <ItemGroup Condition="'$(GenerateDeb)' == 'true'">
      <LinuxPackageDependency Include="libc6;libgcc1;libstdc++6" />
    </ItemGroup>
    <ItemGroup>
      <DebJsonProperty Include="symlinks" Object="{ &quot;dotnet&quot;: &quot;/usr/bin/dotnet&quot; }" />
      <RpmJsonProperty Include="directories" Object="[ &quot;/usr/share/dotnet&quot;, &quot;/usr/share/doc/dotnet-host&quot; ]" />
      <PackageConflictsProperty Include="package_conflicts" Object="[ &quot;dotnet&quot;, &quot;dotnet-nightly&quot; ]" />
      <DebJsonProperty Include="@(PackageConflictsProperty)" />
      <RpmJsonProperty Include="@(PackageConflictsProperty)" />
    </ItemGroup>
  </Target>

  <PropertyGroup>
    <_MacOSIntermediatesPath>$(IntermediateOutputPath)macos/</_MacOSIntermediatesPath>
    <!-- CreatePkg depends on this property being set.-->
    <MacOSScriptsDirectory>$(_MacOSIntermediatesPath)scripts</MacOSScriptsDirectory>
  </PropertyGroup>
  <ItemGroup>
    <_MacOSScript Include="$(MacOSScriptsTemplateDirectory)/*" Destination="$(MacOSScriptsDirectory)/%(FileName)%(Extension)"/>
  </ItemGroup>
  <Target Name="ReplaceTemplateParametersInMacOsScripts" 
          Inputs="@(_MacOSScript)"
          Outputs="@(_MacOSScript->'%(Destination)')"
          DependsOnTargets="_GetInstallerProperties"
          BeforeTargets="CreatePkg">

    <PropertyGroup>
      <_UnameMachineRegex>$(InstallerTargetArchitecture)</_UnameMachineRegex>
      <_UnameMachineRegex Condition="'$(InstallerTargetArchitecture)' == 'arm64'">arm64|aarch64</_UnameMachineRegex>
      <_UnameMachineRegex Condition="'$(InstallerTargetArchitecture)' == 'x64'">amd64|x86_64</_UnameMachineRegex>
      <_MacOSScriptsTemplateProperties>InstallerTargetArchitecture=$(InstallerTargetArchitecture);UnameMachineRegex=$(_UnameMachineRegex)</_MacOSScriptsTemplateProperties>
    </PropertyGroup>
    
    <GenerateFileFromTemplate TemplateFile="@(_MacOSScript)" 
                              OutputPath="@(_MacOSScript->'%(Destination)')"
                              Properties="$(_MacOSScriptsTemplateProperties)">
      <Output TaskParameter="ResolvedOutputPath" ItemName="FileWrites" />
    </GenerateFileFromTemplate>

    <Exec Condition="!$([MSBuild]::IsOSPlatform('windows'))" Command="chmod +x %(_MacOSScript.Destination)" />
  </Target>

  <ItemGroup>
    <PackageReference Include="Microsoft.DotNet.Build.Tasks.Templating" Version="$(MicrosoftDotNetBuildTasksTemplatingVersion)" />
  </ItemGroup>
</Project>
